package com.atguigu.test

import java.text.SimpleDateFormat
import scala.io.Source

object Test1Again {
//需求: 统计每个区域的平均等客时间
def main(args: Array[String]): Unit = {
  //读取文件
  val list = Source.fromFile("datas//data.txt").getLines().toList
    //1.列裁剪
  list.map(line=>{
    val arr = line.split("\t")
    val user = arr(0)
    val fromaddr = arr(1)
    val toaddr = arr(2)
    val fromtime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(arr(3)).getTime
    val totime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(arr(4)).getTime
    (user,toaddr,fromtime,totime)
  })
    //2.user分组
    .groupBy{
      case (user,toaddr,fromtime,totime) => user
    }
      //user1=>list((user,toaddr,fromtime,totime),(user,toaddr,fromtime,totime))
      //user2=>list((user,toaddr,fromtime,totime),(user,toaddr,fromtime,totime))
    //3.map
    .toList.flatMap{
      case (user,list) =>
        val sortlist = list.sortBy(_._3)
        val tuples = sortlist.sliding(2)
       // user1=>list(  list1(user,toaddr,fromtime,totime),(user,toaddr,fromtime,totime)
        //               list2(user,toaddr,fromtime,totime),(user,toaddr,fromtime,totime)
          .map(y=>{
            val toaddr = y(0)._2
            val waittime = (y(1)._3 - y(0)._4)/1000
           ( toaddr,waittime)
          })
        tuples
    }.groupBy{
    case (region,waittime) => region
  }
    .map{
      case (region,list) =>
        val sum = list.map(_._2).sum
        val num = list.size.toDouble
        (region,sum/num)
    }.foreach(println)




}
}
